/****************************************************************************/
/*                                                                          */
/*   FILE:    link.h	                                                    */
/*   DATE:    26/06/97                                                      */
/*   PURPOSE: Header File for DVB-CI Link layer                        		*/
/*   TAB STOPS : 4															*/
/*   HISTORY:				                                    			*/
/*   Version    Date	  	    Written by	     Notes	    	    		*/
/*	 1.0	    13/06/1997      SCM              Initial implementation 	*/
/*	 1.04		31/07/1997		SCM         	 Acces to PHYSICAL Task		*/
/*								instead of PHYSICAL Driver					*/
/*                                                                    	    */
/*--------------------------------------------------------------------------*/
/*   GENERAL COMMENTS                                                 	    */
/*									    									*/
/****************************************************************************/
#ifndef __LINK_H
#define __LINK_H

#include "xmsg.h"

#ifdef __cplusplus
extern "C" {
#endif

/*--------------------------------------------------------------------------*/
/* Symbol Definitions                                                       */
/*--------------------------------------------------------------------------*/
/* states of link state diagram */
#define LN_STAT_NOT_USED	1
#define LN_STAT_IDLE 		2
#define LN_STAT_CONNECTED 	3
#define LN_STAT_SEND		4
#define GETLINKSTATNAME(stat) ((stat)==LN_STAT_NOT_USED?"LN_STAT_NOT_USED":	((stat)==LN_STAT_IDLE?"LN_STAT_IDLE":((stat)==LN_STAT_CONNECTED?"LN_STAT_CONNECTED":((stat)==LN_STAT_SEND?"LN_STAT_SEND":"UNKN_LINKSTAT"))))

/*--------------------------------------------------------------------------*/
/* Structures Definitions                                                   */
/*--------------------------------------------------------------------------*/
typedef struct
{
	unsigned short	m_slotId;			// reference to physical link			
	unsigned char	m_state;			// state of transport connection		       
	unsigned short	m_negoSize;			// physical buffer size					
	unsigned short	m_tcidCount;		// number of tcid						   
	unsigned char	m_tcid;				// wait for LN_ACK on this tcid			
	unsigned char	m_isTpduSending;	// 1 if it exists a sending TPDU, else 0
}lslot_t;        						// informations for one module 						
	
typedef struct
{
	unsigned short	m_slotId;			/* reference to physical link			*/
	unsigned char	m_state;			/* state of transport connection		*/
	unsigned char	m_tcid;			/* transport connection number			*/
	unsigned char*	m_pbTpdu;			/* pointer to TPDU to send to PHYSICAL Driver */
	unsigned char*	m_pbSend;			/* pointer to LPDU to send to PHYSICAL Driver */
	unsigned long	m_sendSize;			/* length of TPDU's part already sent	*/
	unsigned char*	m_pbRecv;			/* pointer to TPDU to send to TRANS Task*/
	unsigned long	m_recvSize;			/* length of TPDU's part already received	  */
}ltcid_t;    		/* informations for one transport connection		*/
	
/*--------------------------------------------------------------------------*/
/* Function Prototypes                                                      */
/*--------------------------------------------------------------------------*/
int  link(void* param);				/* Entry point of LINK Task					*/

int  LINK_Init(void);			/* Initialization Module					*/
void LINK_State(xmsg_t* pXmsg);	        /* Entry point of the Link State Diagram Module */
void LINK_End(void);			/* Ending module 							*/

int  LINK_Connect(xmsg_t* pXmsg);		/* LN_CONNECT received from PHYSICAL driver	*/
int  LINK_Disconnect(xmsg_t* pXmsg);		/* LN_DISCONNECT received from PHYSICAL drv.*/
int  LINK_Error(xmsg_t* pXmsg);			/* LN_ERROR received from PHYSICAL driver	*/
int  LINK_Lpdu(xmsg_t* pXmsg);			/* LPDU received from PHYSICAL driver		*/
int  LINK_Ack(xmsg_t* pXmsg);			/* LN_ACK received from PHYSICAL driver		*/
int  LINK_Tpdu(xmsg_t* pXmsg);			/* TPDU received from TRANS Task			*/

int  LINK_AllocSlotIndex(xmsg_t* pXmsg);                                   /* reserve a new entry in g_lnk_tab_slot   */
int  LINK_FreeSlotIndex(int lnkSlotIndex, unsigned short slotId);		/* free an g_lnk_tab_slot entry			*/
int  LINK_GetSlotIndex(unsigned short slotId);					/* get the current entry in g_lnk_tab_slot */

int  LINK_AllocTcidIndex(int lnkSlotIndex, unsigned short slotId, unsigned char tcid); /* reserve a new entry in g_lnk_tab_tcid   */
int  LINK_GetTcidIndex(unsigned short slotId, unsigned char tcid); /* get the current entry in g_lnk_tab_tcid */

/*--------------------------------------------------------------------------*/
/* State Diagram                                                      */
/*--------------------------------------------------------------------------*/

int LINK_Action(int lnkSlotIndex, unsigned char evt, int nExtraInfo, xmsg_t* pXmsg);			/* Entry Point		*/
int LINK_Idle(int lnkSlotIndex, unsigned char evt);						/* idle State		*/			    
int LINK_Connected(int lnkSlotIndex, unsigned char evt, int nExtraInfo, xmsg_t* pXmsg);		/* CONNECTED State	*/			    
int LINK_Send(int lnkSlotIndex, unsigned char evt, int nExtraInfo, xmsg_t* pXmsg);			/* SEND State		*/			    

int LINK_SendLPDU(int lnkSlotIndex, int tcidIndex, unsigned char tcid, 
				  unsigned char *lpdu, unsigned long len);	/* send a LPDU to PHYSICAL Driver */


/*--------------------------------------------------------------------------*/
/* FIFO management for LINK Task                                              */
/*--------------------------------------------------------------------------*/
#define LNK_MAXFIFOELT 10		/* 10 entries for a fifo 						*/

typedef struct
{
	int			m_head;					/* fifo's head 								*/
	int			m_tail;					/* fifo's tail 								*/ 
	int			m_count;				/* number of elements in fifo				*/
	tran_tpdu_t	m_arItems[LNK_MAXFIFOELT];		/* element's fifo 							*/
}lnk_fifo_t;
	
void LINK_ResetFifo(int tcidIndex);				/* Clear Fifo					*/
int  LINK_PutFifo(int tcidIndex, tran_tpdu_t *ptr);	/* Put a SPDU into the FIFO		*/
int  LINK_GetFifo(int tcidIndex, tran_tpdu_t *ptr);  /* Get a SPDU into the FIFO		*/

#ifdef __cplusplus
}
#endif


#endif	// !__LINK_H
